package com.seeyou.lake.viewmodel

import androidx.lifecycle.MutableLiveData
import com.seeyou.lake.base.myBaseViewModel
import com.seeyou.lake.base.NetUrlConstant.BASE_URL
import com.seeyou.lake.bean.ModelBean
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException


class ModifyViewModel : myBaseViewModel() {
    val imgStreamResponse by lazy { MutableLiveData<ByteArray>() }
    val modelListResponse by lazy { MutableLiveData<List<ModelBean>>() }
    val addArticleResponse by lazy { MutableLiveData<String>() }
    val deleteArticleResponse by lazy { MutableLiveData<Any>() }
    fun toImg(content: String, url: String, type: Int) {

        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url(BASE_URL + "/poetry/tool/toImg?content=$content&url=$url&type=$type")
            .build()
        okHttpClient.newCall(request).enqueue(object : okhttp3.Callback {
            override fun onFailure(call: okhttp3.Call, e: IOException) {

            }

            override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {

                if (response.isSuccessful) {
                    imgStreamResponse.postValue(response.body!!.bytes())

                }
            }

        })
    }

    fun getModelList() {
        netWorkJob(
            successResult = {
                val data = mReo.getModelList()
                if (data.isOk()) {
                    modelListResponse.value = data.data
                } else {
                    toast(data.message)
                }
            }
        )
    }

    fun addArticle(
        uid: String,
        content: String,
        title: String,
        type: Int,
        secret: Int,
        filePath: String
    ) {
        netWorkJob(
            successResult = {
                val data = mReo.addArticle(uid, content, title, type, secret, filePath)
                if (data.isOk()) {
                    addArticleResponse.value = "success"
                } else {
                    addArticleResponse.value = "failed"
                    toast(data.message)
                }
            },
            errorResult = {
                addArticleResponse.value = "failed"
            }

        )
    }


    fun deleteArticle(pid: String) {
        netWorkJob(
            successResult = {
                val data = mReo.deleteArticle(pid)
                if (data.isOk()) {
                    toast("delete success")
                    deleteArticleResponse.value = ""
                } else {
                    toast(data.message)
                }
            }
        )
    }
}